﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

public class Console
{
    // Member vars here
    private SpriteFont m_font;
    private List<Entry> m_entryList;

    public const int kMaxEntries = 10;
    private static int kConsoleWidth = 50;  // Width in pixels is this * 8.

    /**
     * Our constructor.
     */
    public Console(ContentManager content)
    {
        m_entryList = new List<Entry>();
        m_font = content.Load<SpriteFont>("fonts/Console");
    }

    /**
     * Adds the given string to our list for drawing.
     */
    public void print(String text, Color color, Color bgColor)
    {
        // Make sure we always have less than our max entry amount
        while (m_entryList.Count >= kMaxEntries)
        {
            m_entryList.RemoveAt(0);
        }
        m_entryList.Add(new Entry(text, color, bgColor));
    }
    /**
     * Adds given string with default background of Black.
     */
    public void print(String text, Color color)
    {
        print(text, color, Color.Black);
    }
    /**
     * Adds given string with default color of White over Black.
     */
    public void print(String text)
    {
        print(text, Color.White, Color.Black);
    }

    /**
     * Updates all entries we are displaying.
     */
    public void update(double dt)
    {
        List<Entry> deadList = new List<Entry>();

        // Update each Entry, also making note of which need to be removed.
        foreach(Entry e in m_entryList)
        {
            if (!e.update(dt))
            {
                deadList.Add(e);
            }
        }

        // Remove each dead/invisible entry
        foreach (Entry e in deadList)
        {
            m_entryList.Remove(e);
        }
    }

    /**
     * Displays each entry
     */
    public void render()
    {
        if (!Game.kConsoleOn)
        {
            return;
        }

        Texture2D texture = Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.OUTLINE).get("outline").spriteSheet;
        int count = 0;
        foreach (Entry e in m_entryList)
        {
            Game.getGame().getSpriteBatch().Draw(texture, new Vector2(0, 16 * count), null, e.m_bgColor, 0.0f, new Vector2(0, 0),
                new Vector2(kConsoleWidth, 2), SpriteEffects.None, Priority.kConsoleFar);
            Game.getGame().getSpriteBatch().DrawString(m_font, e.m_text, new Vector2(0, 16 * count), e.m_color, 0.0f, new Vector2(0,0),
                1.0f, SpriteEffects.None, Priority.kConsoleNear);
            count++;
        }

    }

    /**
     * Basically a struct for storing our display informations.
     */
    private class Entry
    {
        public String m_text;
        public Color m_bgColor;
        public Color m_color;
        public double m_lifetime;
        public static double kEntryLifetime = 5000.0;

        public Entry(String text, Color color, Color bgColor)
        {
            m_text = text;
            m_bgColor = bgColor;
            m_color = color;
            m_lifetime = kEntryLifetime;
        }

        /**
         * Update the lifetime/colors/etc of this Entry.
         * Returns false once this should be deleted.
         */
        public bool update(double dt)
        {
            m_lifetime -= dt;
            // Our transparency's a function of the time we have left
            m_color.A = (byte)(0xFF * (m_lifetime / kEntryLifetime));
            if (m_lifetime < 0.0)
            {
                return false;
            }
            return true;
        }
    }
}
